package com.wave.gate.init;



import lombok.extern.slf4j.Slf4j;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

@Slf4j
public class ByteUtil {

    static {
        System.loadLibrary("native-lib");
    }

    public native static FileDescriptor open(String path, int port, int flag);

    public native static void close();

    /**
     * 字节数组转字符串
     *
     * @param b          要转换的字节数组
     * @param charEncode 字符编码，如：UTF-8、GB2312，UTF-8编码一个汉字占三个字节，GB2312一个汉字占两个字节
     * @return 字符串
     */
    public static String byteToString(byte[] b, String charEncode) {
        String destObj = null;
        try {
            destObj = new String(b, charEncode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return destObj.replaceAll("\0", " ");
    }

    /**
     * 字符串转字节数组
     *
     * @param str        要转换的字符串
     * @param charEncode 字符编码，如：UTF-8、GB2312，UTF-8编码一个汉字占三个字节，GB2312一个汉字占两个字节
     * @return 字节数组
     */
    public static byte[] stringToByte(String str, String charEncode) {
        byte[] destObj = null;
        try {
            if (null == str || str.trim().equals("")) {
                destObj = new byte[0];
                return destObj;
            } else {
                destObj = str.getBytes(charEncode);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return destObj;
    }

    public static boolean writeSerialPort(OutputStream os, byte[] mBuffer) {
        boolean result = false;
        if (os != null) {
            try {
                os.write(mBuffer);
                result = true;
                log.error("writeSerialPort:发送成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static boolean writeSerialPort(OutputStream os, String cmd) {
        byte[] mBuffer = hexStringToBytes(cmd);
        return writeSerialPort(os, mBuffer);
    }

    public static byte[] hexStringToBytes(String strHex) {
        int len = (strHex.length() / 2);
        byte[] result = new byte[len];
        char[] chars = strHex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(chars[pos]) << 4 | toByte(chars[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static String bytesToHexString(byte[] b) {
        StringBuilder sbu = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sbu.append(hex.toUpperCase());
        }
        return sbu.toString();
    }

    public static byte checkSum(byte[] inBytArr, int byteCount) {
        byte result = 0;
        for (int i = 0; i < byteCount; ++i) {
            result ^= inBytArr[i];
        }
        return result;
    }

}
